﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PSL.Package.System
{
    public class PSL_String
    {
        /// <summary>
        /// Init String
        /// </summary>
        /// <param name="Runtime">Host</param>
        internal static void Init(PSLRuntime Runtime)
        {            
            // String
            ///Runtime.AddMethod("tolower", PSL_String.PSL_ToLower);
            ///Runtime.AddMethod("toupper", PSL_String.PSL_ToUpper);
            ///Runtime.AddMethod("indexof", PSL_String.PSL_IndexOf);
            ///Runtime.AddMethod("substr", PSL_String.PSL_Substring);
            ///Runtime.AddMethod("concat", PSL_String.PSL_Concat);
            ///Runtime.AddMethod("len", PSL_String.PSL_Length);
        }

        /// <summary>
        /// String To Lower
        /// </summary>
        /// <param name="FunctionName"></param>
        /// <param name="Parameter"></param>
        /// <returns></returns>
        internal static string PSL_ToLower(string FunctionName, PSLParameter Parameter)
        {
            string cToReturn = "";

            if (Parameter.ContainsParameter("text"))
            {
                cToReturn = Parameter["text"].ToString().ToLower();
            }

            return cToReturn;
        }

        /// <summary>
        /// To Upper
        /// </summary>
        /// <param name="FunctionName"></param>
        /// <param name="Parameter"></param>
        /// <returns></returns>
        internal static string PSL_ToUpper(string FunctionName, PSLParameter Parameter)
        {
            string cToReturn = "";

            if (Parameter.ContainsParameter("text"))
            {
                cToReturn = Parameter["text"].ToString().ToUpper();
            }

            return cToReturn;
        }

        /// <summary>
        /// Substring
        /// </summary>
        /// <param name="FunctionName"></param>
        /// <param name="Parameter"></param>
        /// <returns></returns>
        internal static string PSL_Substring(string FunctionName, PSLParameter Parameter)
        {
            string cToReturn = "";

            if (Parameter.ContainsParameter("text", "start", "len"))
            {
                int cStart = 0;
                int cLen = 0;
                if (int.TryParse(Parameter["start"].ToString(), out cStart) && int.TryParse(Parameter["len"].ToString(), out cLen))
                {
                    cToReturn = Parameter["text"].ToString().Substring(cStart, cLen);
                }
            }

            return cToReturn;
        }

        /// <summary>
        /// Index Of
        /// </summary>
        /// <param name="FunctionName"></param>
        /// <param name="Parameter"></param>
        /// <returns></returns>
        internal static dynamic PSL_IndexOf(string FunctionName, PSLParameter Parameter)
        {
            int cToReturn = -1;

            if (Parameter.ContainsParameter("text", "value"))
            {
                if (Parameter["text"] is string)
                {
                    cToReturn = Parameter["text"].ToString().IndexOf(Parameter["value"].ToString());
                }
            }

            return cToReturn;
        }

        /// <summary>
        /// Concat string
        /// </summary>
        /// <param name="FunctionName"></param>
        /// <param name="Parameter"></param>
        /// <returns></returns>
        internal static string PSL_Concat(string FunctionName, PSLParameter Parameter)
        {
            string cToReturn = "";

            foreach (var value in Parameter)
            {
                cToReturn += value.Value;
            }

            return cToReturn;
        }

        /// <summary>
        /// String Length
        /// </summary>
        /// <param name="FunctionName"></param>
        /// <param name="Parameter"></param>
        /// <returns></returns>
        internal static string PSL_Length(string FunctionName, PSLParameter Parameter)
        {
            string cToReturn = "";

            if (Parameter.ContainsParameter("text"))
            {
                cToReturn = Parameter["text"].ToString().Length.ToString();
            }

            return cToReturn;
        }
    }
}
